<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    const fn1 = (nums, target) => {
        let activeIndex = 0;
        const res = [];
        const loopFun = (activeNum, findNum, _nums) => {
            const findIndex = _nums.findIndex(num => num === findNum);
            if (findIndex > -1) {
                res.push(activeIndex, findIndex + activeIndex + 1);
            } else if (_nums.length >= 2) {
                activeIndex++;
                loopFun(nums[activeIndex], target - nums[activeIndex], nums.slice(activeIndex + 1));
            }
        }

        loopFun(nums[activeIndex], target - nums[activeIndex], nums.slice(activeIndex + 1));
        return res;
    };

    const fn2 = (nums, target) => {
        if (nums.length === 2) {
            return [0, 1];
        }
        const dealData = nums.map((value, index) => {
            return {
                value,
                index
            }
        }).sort((a, b) => a.value - b.value);
        const res = [];
        let activeIndex = 0;
        const loop = (numArr, activeData) => {
           const {value: activeValue} = activeData;
           const findValue = target - activeValue;
           const numLen = numArr.length;
           let findStart = 0;
           let hasFind = false;
           while (!hasFind && findStart <= numLen ) {
               if (numArr[findStart]?.value === findValue) {
                   hasFind = true;
               } else {
                   findStart++;
               }
           }
            if (hasFind) {
                res.push(dealData[activeIndex].index, numArr[findStart].index)
           } else if(numArr.length) {
               activeIndex++;
               loop(numArr.slice(1), numArr[0])
           }
        };

        loop(dealData.slice(activeIndex + 1), dealData[activeIndex]);
        return res;
    }

    const fn3 = (nums, target) => {
        const dataMap = new Map();
        const res = [];
        const dealData = (numsArr, targetValue) => {
            const numLen = numsArr.length;
            for (let index = 0; index < numLen; index++) {
                const findValue = targetValue - numsArr[index];
                if (dataMap.has(findValue)) {
                    const findIndex = dataMap.get(findValue);
                    res.push(findIndex, index);
                    break ;
                } else {
                    dataMap.set(numsArr[index], index)
                }
            }

        };
        dealData(nums, target);
        return res;
    }

    const run = (nums, target, res) => {
        const fnList = [fn1, fn2,fn3];
        const resultArr = fnList.map((fnItem, index) => {
            console.time(`fn${index + 1}`);
            const r1 = fnItem(JSON.parse(JSON.stringify(nums)), target);
            console.timeEnd(`fn${index + 1}`);
            const result1 = r1.sort((a, b) => a - b);
            return result1[0] === res[0] && result1[1] === res[1];
        })
        return !resultArr.includes(false);
    };

    const eg = [
        {
            nums: [3, 3],
            target: 6,
            res: [0, 1]
        },
        {
            nums: [3, 2, 4],
            target: 6,
            res: [1, 2]
        },
        {
            nums: [2, 7, 11, 16],
            target: 9,
            res: [0, 1]
        },
        {
            nums: [3, 2, 3],
            target: 6,
            res: [0, 2]
        },
        {
            nums: [1, 6142, 8192, 10239],
            target: 18431,
            res: [2, 3]
        },
        {
            nums: [0,3,-3,4,-1],
            target: -1,
            res: [0, 4]
        },
        {
            nums: [1,3,4,2],
            target: 6,
            res: [2, 3]
        }
    ]

    const init = () => {
        const pass = [];
        const notPass = [];
        eg.forEach(({nums, target, res}, index) => {
            const r = run(nums, target, res);
            if (r) {
                pass.push(index)
            } else {
                notPass.push(index)
            }
        });
        if (pass.length === eg.length) {
            console.log('全部通过')
        } else {
            console.log(`通过实例：${pass.length}`, pass);
            console.log(`未用过实例：${notPass.length}`, notPass);
        }
    };

    init();

</script>
</html>
